package View.Updata;

import Hander.Add.AddDeptViewHander;
import Hander.Updata.UpdataDeptViewHander;
import View.LeaderView;
import entity.Department;
import entity.Personnel;
import service.PersonnelService;
import service.PersonnelServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class UpdataDeptView extends JDialog {
    private String tableName="dept";
    private JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20));

    // 添加部门信息
    JLabel departmentIDLabel = new JLabel("DepartmentID:", JLabel.RIGHT);
    JTextField departmentIDTextField = new JTextField();
    JLabel departmentNameLabel = new JLabel("DepartmentName:", JLabel.RIGHT);
    JTextField departmentNameTextField = new JTextField();
    JLabel departmentHeadLabel = new JLabel("DepartmentHead:", JLabel.RIGHT);
    JTextField departmentHeadTextField = new JTextField();
    JLabel departmentAddressLabel = new JLabel("DepartmentAddress:", JLabel.RIGHT);
    JTextField departmentAddressTextField = new JTextField();
    JLabel departmentPostalCodeLabel = new JLabel("DepartmentPostalCode:", JLabel.RIGHT);
    JTextField departmentPostalCodeTextField = new JTextField();
    JLabel departmentPhoneNumberLabel = new JLabel("DepartmentPhoneNumber:", JLabel.RIGHT);
    JTextField departmentPhoneNumberTextField = new JTextField();

    JButton addBtn = new JButton("修改");
    UpdataDeptViewHander updataDeptViewHander;

    public UpdataDeptView(LeaderView leaderView, int key) {
        super(leaderView, "修改信息", true);

        // 回显功能
        PersonnelService personnelService=new PersonnelServiceImpl();
        Vector<Object> array = personnelService.getBy(key, "dept");

        updataDeptViewHander = new UpdataDeptViewHander(this, leaderView);
        departmentIDLabel.setPreferredSize(new Dimension(120, 20));
        jPanel.add(departmentIDLabel);
        departmentIDTextField.setPreferredSize(new Dimension(200, 20));
        jPanel.add(departmentIDTextField);
        departmentIDTextField.setText(""+array.get(0));

        departmentNameLabel.setPreferredSize(new Dimension(120, 20));
        jPanel.add(departmentNameLabel);
        departmentNameTextField.setPreferredSize(new Dimension(200, 20));
        jPanel.add(departmentNameTextField);
        departmentNameTextField.setText(""+array.get(1));

        departmentHeadLabel.setPreferredSize(new Dimension(120, 20));
        jPanel.add(departmentHeadLabel);
        departmentHeadTextField.setPreferredSize(new Dimension(200, 20));
        jPanel.add(departmentHeadTextField);
        departmentHeadTextField.setText(""+array.get(2));

        departmentAddressLabel.setPreferredSize(new Dimension(120, 20));
        jPanel.add(departmentAddressLabel);
        departmentAddressTextField.setPreferredSize(new Dimension(200, 20));
        jPanel.add(departmentAddressTextField);
        departmentAddressTextField.setText(""+array.get(3));

        departmentPostalCodeLabel.setPreferredSize(new Dimension(120, 20));
        jPanel.add(departmentPostalCodeLabel);
        departmentPostalCodeTextField.setPreferredSize(new Dimension(200, 20));
        jPanel.add(departmentPostalCodeTextField);
        departmentPostalCodeTextField.setText(""+array.get(4));

        departmentPhoneNumberLabel.setPreferredSize(new Dimension(120, 20));
        jPanel.add(departmentPhoneNumberLabel);
        departmentPhoneNumberTextField.setPreferredSize(new Dimension(200, 20));
        jPanel.add(departmentPhoneNumberTextField);
        departmentPhoneNumberTextField.setText(""+array.get(5));

        addBtn.addActionListener(updataDeptViewHander);
        jPanel.add(addBtn);

        Container contentPane = getContentPane();
        contentPane.add(jPanel);

        setSize(400, 350);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public Department buildDepartment() {
        Department department = new Department();
        department.setDepartmentID(Integer.valueOf(departmentIDTextField.getText()));
        department.setDepartmentName(departmentNameTextField.getText());
        department.setDepartmentHead(departmentHeadTextField.getText());
        department.setDepartmentAddress(departmentAddressTextField.getText());
        department.setDepartmentPostalCode(departmentPostalCodeTextField.getText());
        department.setDepartmentPhoneNumber(departmentPhoneNumberTextField.getText());
        return department;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
}
